P1B: Refactor (packages/ui/src/theme/resolve.ts): Function with high complexity - #37
Open
MohAlbader wants to merge 3 commits into
Open
P1B: Refactor (packages/ui/src/theme/resolve.ts): Function with high complexity#37MohAlbader wants to merge 3 commits into
MohAlbader wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Issue
Link to the associated GitHub issue: #34
Full path to the refactored file:
packages/ui/src/theme/resolve.tsWhat do you think this file does? Turns a theme definition (seed or palette colors) into the full set of CSS design tokens the UI renders with, in both light and dark mode.
What is the scope of your refactoring within that file? The inline closures
lum,hit,on,content, andmodifiedinsideresolveThemeVariant.Which Qlty-reported issue did you address?
resolveThemeVariantresolveThemeVariant2. Refactoring
How did the specific issue you chose impact the codebase's maintainability? One 530-line function mixed general color math with token assembly. The helpers were trapped in the closure, so they couldn't be reused or tested without resolving an entire theme.
What changes did you make to resolve the issue? Extracted
relativeLuminance,contrastRatio,readableTextOn,contentTone,hueDistance, andmodifiedDiffToneto module level, each takingisDarkand its other dependencies as explicit parameters. Keptconst on = readableTextOnso the ~25 existing call sites are untouched.How do your changes improve maintainability? Did you consider alternatives? The helpers are now independently testable and clearly named; the "many returns" smell is gone and complexity dropped 243 → 219. I considered also splitting the token assembly into per-category builders, but that's ~250 lines with no behavioral tests protecting it, so the risk wasn't worth it here.
3. Validation
How did you validate that the change is correct? Added
packages/ui/src/theme/resolve.test.ts— 22 tests covering each extracted helper directly (black-on-white contrast = 21:1, hue wraparound, bothmodifiedDiffTonebranches) plus end-to-endresolveThemeVariantcalls for seeds and palette variants in both modes.packages/uiwent from 27 to 49 passing, 0 failures, with 96.84% line coverage onresolve.ts.Screenshot of
bun lintandbun testpassing locallybun lintreports 2 errors, both pre-existing in files this PR does not touch (packages/opencode/src/plugin/xai.tsand an unrelated test file). No lint issues are reported inpackages/ui/src/theme/resolve.ts. CI lint/typecheck checks pass.